package com.example.pc.adapterdemo.utils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * 从map里面取一个字段串类型的值
 * 深度解析，将每个字段都解析出来，放在对应的字段中（更改对应值，比较简单）
 */
public class JsonMapBean extends HashMap<String,Object> implements Serializable {
	private static final long serialVersionUID = 7987461225579121548L;
	public JsonMapBean() {
		super();
	}

	/**
	 * 从map里面取一个字段串类型的值
	 * @param key
	 * @return
	 */
	public String getString(String key) {
		return getString(key,null);
	}

	/**
	 * 从map里面取一个字段串类型的值,不存在时用默认值代替
	 * @param key
	 * @param defaultValue 不存在时的默认值
	 * @return
	 */
	public String getString(String key, String defaultValue) {
		Object v = this.get(key);
		if (v == null) {
			return defaultValue;
		}
		return v.toString();
	}

	public Integer getInt(String key) {
		return getInt(key, null);
	}

	public Integer getInt(String key, Integer defaultValue) {
		Object v = this.get(key);
		if(v == null) {
			return defaultValue;
		}
		return v instanceof Double ? ((Double)v).intValue() :
				v instanceof String ? Integer.parseInt(v.toString()) :
						v instanceof Integer ? ((Integer)v) : defaultValue;
	}

	public Long getLong(String key) {
		return getLong(key, null);
	}

	public Long getLong(String key, Long defaultValue) {
		Object v = this.get(key);
		if(v == null) {
			return defaultValue;
		}
		return Long.parseLong(v.toString());
	}

	public Double getDouble(String key) {
		return getDouble(key,null);
	}

	public Double getDouble(String key, Double defaultValue) {
		Object v = this.get(key);
		if(v == null) {
			return defaultValue;
		}
		/*return v instanceof Double ? ((Double)v) :
				v instanceof String ? Double.parseDouble(v.toString()) : defaultValue;*/
		return (double) (new Integer( (int) v).intValue()) ;
	}

	public Boolean getBoolean(String key){
		return getBoolean(key,null);
	}

	public Boolean getBoolean(String key,Boolean defaultValue){
		Object v = this.get(key);
		if(v == null){
			return defaultValue;
		}
		return v instanceof Boolean ? ((Boolean)v) :
				v instanceof String ? Boolean.parseBoolean(v.toString()) : defaultValue;
	}

	/**
	 * 从参数对象中获取一个列表
	 * @param key
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<JsonMapBean> getList(String key) {
		Object obj = get(key);
		if(obj == null) {
			return new ArrayList<>();
		}
		if(obj instanceof List){
			return (List<JsonMapBean>)obj;
		}
		return new ArrayList<>();
	}

	/***
	 * 从参数对象中获取一个JsonMapBean对象
	 * @param key
	 * @return
	 */
	public JsonMapBean getBean(String key) {
		Object obj = get(key);
		if(obj == null) {
			return new JsonMapBean();
		}
		if(obj instanceof JsonMapBean) {
			return (JsonMapBean) obj;
		}
		return new JsonMapBean();
	}


	@Override
	public String toString() {
		return super.toString();
	}

	/**
	 * 将对象转换成JSON数据格式的字符串
	 * @return
	 */
	public String toJson() {
		JSONObject jsonObject = new JSONObject(this);
		return jsonObject.toString();
	}

	/**
	 * 将JSON格式字符串转换为JsonMapBean对象
	 * @param jsonStr:格式数据 如:{"username":"administartor"}
	 * @return
	 */
	public static JsonMapBean fromJson(String jsonStr){
		JsonMapBean bean = new JsonMapBean();
		if(jsonStr!=null&&jsonStr.length()>0) {
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(jsonStr);
				if (jsonObject != null) {
					Iterator it = jsonObject.keys();
					while (it.hasNext()) {
						String key = String.valueOf(it.next());
						Object value = jsonObject.get(key);
						if(value instanceof JSONObject){
							bean.put(key, fromJson(jsonObject.getString(key)));
						}else if(value instanceof JSONArray){
							JSONArray jsonArray = (JSONArray)value;
							List<JsonMapBean> list = new ArrayList<>();
							for (int i = 0; i < jsonArray.length(); i++) {
								list.add(fromJson(jsonArray.get(i).toString()));
							}
							bean.put(key, list);
						}else{
							bean.put(key, value);
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return bean;
	}
}

